Skip to content

fix(plugin): opencode run waits for plugins to finish before stopping process#23381

Open
0byte-coding wants to merge 6 commits intoanomalyco:devfrom
0byte-coding:fix/plugin-exit-wait
Open

fix(plugin): opencode run waits for plugins to finish before stopping process#23381
0byte-coding wants to merge 6 commits intoanomalyco:devfrom
0byte-coding:fix/plugin-exit-wait

Conversation

@0byte-coding
Copy link
Copy Markdown

Issue for this PR

Closes #23380

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

  • opencode run didn't wait for plugins to finish processing events before exiting
  • Added OPENCODE_EXPERIMENTAL_PLUGIN_EXIT_DEFAULT_TIMEOUT_MS env var for timeout control
  • Track active plugin event handlers with pendingEvents Set<Promise>
  • Made event loop awaitable by storing promise in run.ts
  • Call waitForPendingEvents() when session.idle received
  • Await loop to complete before execute() returns
  • Default 60s timeout allows plugins sufficient time to complete
  • Why it works: Wrapping plugin event handlers in tracked promises lets us await completion before process exit

How did you verify your code works?

  • ✅ All 4/4 plugin messages printed (was 1/4 before fix)
  • ✅ Default 60s timeout allows plugins to complete
  • ✅ Custom timeout via env var works correctly
  • ✅ Unit tests pass and catch regressions
  • ✅ End-to-end test confirms fix works in production scenario
  • ✅ Test verified to fail when fix disabled (simulating bug)
  • ✅ Test verified to pass when fix enabled (catching future regressions)

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@OliverGalusinski
Copy link
Copy Markdown

looks good

- Track pending plugin event handlers in plugin service
- Add OPENCODE_EXPERIMENTAL_PLUGIN_EXIT_DEFAULT_TIMEOUT_MS env var (default: 60000ms)
- Wait for pending plugin events when session becomes idle
- Await event loop completion in run command before exiting

Fixes issue where plugins were interrupted mid-execution when
opencode-dev run exits, causing incomplete plugin processing.
Adds test to ensure plugins complete their event handlers before
opencode exits. Test verifies:
- Plugin event handlers complete within timeout
- Timeout mechanism works correctly

Test catches the regression by failing when the event tracking
or waitForPendingEvents mechanism is disabled.
@0byte-coding 0byte-coding force-pushed the fix/plugin-exit-wait branch from 5269472 to a2fada5 Compare April 23, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: opencode run exits before plugins finish processing events

2 participants